package com.mall.shopping.services;

import com.mall.shopping.ShoppingCartService;
import com.mall.shopping.converter.ShoppingCartToResponse;
import com.mall.shopping.converter.TbItemConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ShoppingCart;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: lin
 * @Date: 2020/7/10 21:12
 */

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private TbItemConverter itemConverter;

    @Autowired
    private ShoppingCartToResponse cartToResponse;


    @Override
    public Boolean addGoodToCart(AddShoppingCartRequest request) {

        try {
            request.requestCheck();
        } catch (Exception e) {
            return false;
        }
        Item item = itemMapper.selectByPrimaryKey(request.getProductId());
        ShoppingCart shoppingCart = itemConverter.ItemToCart(item);
        shoppingCart.setProductNum(request.getProductNum());
        shoppingCart.setChecked("true");
        store(request.getUserId(), shoppingCart);
        return true;
    }

    @Override
    public List<GetShoppingCartResponse> getCartList(Integer uid) {
        String id = "shopping_cart_user_id_" + uid;
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> shoppingCart = bucket.get();
        if (shoppingCart == null){
            return null;
        }
        return cartToResponse.cartToResponseList(shoppingCart);
    }
    //将购物车商品信息修改
    @Override
    public void updateCart(UpdateCartNumRequest updateCartNumRequest) {
        String id = "shopping_cart_user_id_" + updateCartNumRequest.getUserId();
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();
        if (cart == null){
            cart = new LinkedList<>();
        }
        for (int i = 0; i < cart.size(); i++) {
            if (cart.get(i).getProductId().compareTo(updateCartNumRequest.getProductId()) == 0){
                ShoppingCart shoppingCart = cart.get(i);
                shoppingCart.setChecked(updateCartNumRequest.getChecked());
                shoppingCart.setProductNum(updateCartNumRequest.getProductNum());
            }
        }
        bucket.set(cart);

    }
    //将购物车商品信息删除
    @Override
    public void deleteCart(DeleteCartItemRequest deleteCartItemRequest) {
        String id = "shopping_cart_user_id_" + deleteCartItemRequest.getUserId();
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();
        if (cart == null){
            cart = new LinkedList<>();
        }
        for (int i = 0; i < cart.size(); i++) {
            if (cart.get(i).getProductId().compareTo(deleteCartItemRequest.getProductId()) == 0){
                cart.remove(i);
            }
        }
        bucket.set(cart);
    }
    //将购物车商品信息全选
    @Override
    public void itemsCart(ItemsCIartRequst itemsCartRequst) {
        String id = "shopping_cart_user_id_" + itemsCartRequst.getUserId();
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();
        if (cart == null){
            cart = new LinkedList<>();
        }
        for (int i = 0; i < cart.size(); i++) {
            ShoppingCart shoppingCart = cart.get(i);
            shoppingCart.setChecked(itemsCartRequst.getChecked());
        }
        bucket.set(cart);
    }
    //将购物车商品信息全选的删除
    @Override
    public void deleteItemsCart(DeleteCheckedItemRequest deleteCheckedItemRequest) {
        String id = "shopping_cart_user_id_" + deleteCheckedItemRequest.getUserId();
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();
        if (cart == null){
            cart = new LinkedList<>();
        }
        for (int i = 0; i < cart.size(); i++) {
            ShoppingCart shoppingCart = cart.get(i);
            if (shoppingCart.getChecked().equals(true)){
                cart.remove(i);
            }
        }
        bucket.set(cart);
    }


    //将商品信息存储到redis的购物车表中
    private void store(String userId, ShoppingCart shoppingCart) {
        String id = "shopping_cart_user_id_" + userId;
        //获取redis中的购物车列表
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();
        if (cart == null){
            cart = new LinkedList<>();
        }
        boolean content = false;
        for (ShoppingCart good : cart) {
            if (good.getProductId().compareTo(shoppingCart.getProductId()) == 0){
                Integer num = good.getProductNum();
                good.setProductNum(num + shoppingCart.getProductNum());
                content = true;
            }
        }
        if (!content){
            cart.add(shoppingCart);
        }
        bucket.set(cart);
    }


    //订单部分的清空购物车
    @Override
    public void deleteSomeCart(Long userId, List<Long> item) {
        String id = "shopping_cart_user_id_" + userId;
        //获取redis中的购物车列表
        RBucket<List<ShoppingCart>> bucket = redissonClient.getBucket(id);
        List<ShoppingCart> cart = bucket.get();

        if (cart == null){
            return;
        }

        for (int i = 0; i < cart.size(); i++) {
            for (Long aLong : item) {
                if (cart.get(i).getProductId() == aLong.intValue()){
                    cart.remove(i);
                }
            }
        }
        bucket.set(cart);
    }
}
